﻿using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace ServBus.ListForm2010.Utility
{
    public class DecodeUrl
    {
        static int GetCharNum(char ch)
        {
            if (ch >= '0' && ch <= '9') return (int)(ch - '0');
            else return (int)(ch - 'a') + 10;
        }

        public static string Decode(string url)
        {
            Dictionary<string, string> map = new Dictionary<string, string>();
            Regex re = new Regex("\\\\u[0-9a-f]{4}", RegexOptions.IgnoreCase);
            foreach (Match match in re.Matches(url))
            {
                string codeStr = match.Value.Substring(2).ToLower();
                int code = GetCharNum(codeStr[0]) * 4096 + GetCharNum(codeStr[1]) * 256 + GetCharNum(codeStr[2]) * 16 + GetCharNum(codeStr[3]);
                map[match.Value] = ((char)code).ToString();
            }
            foreach (string key in map.Keys)
                url = url.Replace(key, map[key]);
            return url;
        }
    }
}
